<template>
  <div class="www">
    <el-card id="box" v-for="(item, index) in helps">
      <div slot="header" class="clearfix">
        <span>我的求助</span>
      </div>
      <div id="a">
        <el-button type="text" @click="update(item, index)">修改</el-button>
      </div>
      <div class="text item">求助类型：{{ item.type }}</div>
      <div class="text item">求助人电话：{{ item.phone }}</div>
      <div class="text item">地点：{{ item.location }}</div>
      <div class="text item1">求助描述：{{ item.detail }}</div>
      <span>
        <el-button id="b" type="primary" round @click="finishHelp(item, index)"
          >已完成</el-button
        ></span
      >
      <span
        ><el-button
          id="bbbb"
          type="primary"
          round
          icon="el-icon-delete"
          @click="deleteHelp(item, index)"
          >取消</el-button
        ></span
      >
    </el-card>

    <el-dialog title="修改框" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="求助类型" :label-width="formLabelWidth">
          <el-input v-model="helpInfo.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="求助电话" :label-width="formLabelWidth">
          <el-input v-model="helpInfo.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地点" :label-width="formLabelWidth">
          <el-input v-model="helpInfo.location" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="详细描述" :label-width="formLabelWidth">
          <el-input v-model="helpInfo.detail" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getMyHelps } from "../services/help";
import { updateHelps } from "../services/help";
import { finishHelp } from "../services/help";
import { deleteHelp } from "../services/help";
export default {
  data() {
    return {
      helpInfo: {
        type: "",
        location: "",
        phone: "",
        detail: "",
        helpid: null,
      },
      helps: [],
      editIndex: null,
      formLabelWidth: "120px",
      dialogFormVisible: false,
    };
  },
  methods: {
    update(item, index) {
      this.$data.dialogFormVisible = true;
      this.$data.helpInfo.helpid = item.helpid;
      this.$data.helpInfo.type = item.type;
      this.$data.helpInfo.location = item.location;
      this.$data.helpInfo.phone = item.phone;
      this.$data.helpInfo.detail = item.detail;
    },
    confirm: function () {
      updateHelps(this.helpInfo)
        .then((res) => {
          this.dialogFormVisible = false;
          getMyHelps({ helpee_id: this.$store.state.account.user.id }).then(
            (res) => {
              this.helps = res.data.result;
            }
          );
        })
        .catch((err) => {
          console.log(err);
        });
    },
    finishHelp: function (item, index) {
      this.helps.status = 1;
      this.helpid = item.helpid;
      this.$confirm("你确定已完成帮助了吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          finishHelp({ status: this.helps.status, helpid: this.helpid }).then(
            (res) => {
              getMyHelps({ helpee_id: this.$store.state.account.user.id }).then(
                (res) => {
                  this.helps = res.data.result;
                }
              );
              this.$message({
                type: "success",
                message: "帮助成功!",
              });
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    deleteHelp: function (item, index) {
      this.helpid = item.helpid;
      this.$confirm("你确定要删除帮助了吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          deleteHelp({ helpid:this.helpid }).then(
            (res) => {
              getMyHelps({ helpee_id: this.$store.state.account.user.id }).then(
                (res) => {
                  this.helps = res.data.result;
                }
              );
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
  },
  mounted: function () {
    getMyHelps({ helpee_id: this.$store.state.account.user.id }).then((res) => {
      this.helps = res.data.result;
    });
  },
};
</script>


<style scoped>
#bbbb {
  text-align: center;
  margin-left: 80px;
}
#b {
  text-align: center;
  margin-left: 20px;
}
.text {
  font-size: 15px;
  font-weight: bold;
  color:#131212
}
.item {
  margin-bottom: 22px;
}
.item1 {
  margin-bottom: 100px;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
#box {
  width: 400px;
  border-radius: 20px;
  background-color: rgb(243, 233, 227);
  width: 330px;
  height: 420px;
  border: 2px solid #eeae9e;
  margin-left: 20px;
  margin: 20px;
}
#a {
  float: right;
}
.www {
  display: flex;
  flex-wrap: wrap;
  background: url("../assets/bg12.jpg");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
}
.el-card ::v-deep .el-card__header {
  padding: 20px 20px;
  background-color: rgb(9, 159, 197);
  color:rgb(252, 252, 252);
  font-size: 25px;
  font-weight: bold;
}
</style>